package com.cgbl.cg.vm.collection

import android.app.Application
import api.WorkServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.model.CollectDetailBean
import com.robot.base.model.PageListBean
import com.robot.base.model.WorksBean
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class SelectCollectionViewModel(application: Application): GLBaseViewModel(application) {
    val uc = UiChangeEvent()

    var pageNo = 1

    class UiChangeEvent {
        val refreshEvent = SingleLiveEvent<Int>()
        val refreshDataEvent = SingleLiveEvent<List<CollectDetailBean>>()
    }
    fun refresh() {
        pageNo = 1
        collectPage()
    }

    fun next() {
        pageNo++
        collectPage()
    }
    fun collectPage(){
        WorkServiceFactory.collectPage(
            mapOf(
                "pageNo" to pageNo,
                "pageSize" to "20",
                "isAccount" to "0",
            )
        ).subscribe(object : AbstractViewModelSubscriber<BaseResponse<PageListBean<CollectDetailBean>>>(this){
            override fun onNext(t: BaseResponse<PageListBean<CollectDetailBean>>) {
                uc.refreshEvent.postValue(
                    if (t.data.total.toInt() < pageNo * 20) {
                        0
                    } else {
                        1
                    }
                )
                uc.refreshDataEvent.postValue(t.data.list)

            }

            override fun onError(e: Throwable) {
                super.onError(e)
                uc.refreshEvent.postValue(0)
            }
        })
    }
}